package pt.inescid.components.policyengine.heimdall.core;

import java.io.IOException;
import java.util.Vector;

import pt.inescid.components.policyengine.heimdall.base.EventObj;
import pt.inescid.components.policyengine.heimdall.base.User;
import pt.inescid.components.policyengine.heimdall.contact.Outbound;
import pt.inescid.components.policyengine.heimdall.contact.RemoteCommand;
import pt.inescid.components.policyengine.heimdall.database.PolicyDAO;
import pt.inescid.components.policyengine.heimdall.pap.PolicyManager;
import pt.inescid.components.policyengine.heimdall.pap.base.CesDefinition;
import pt.inescid.components.policyengine.heimdall.pap.base.ModelDefinition;
import pt.inescid.components.policyengine.heimdall.pap.base.RuleDefinition;
import pt.inescid.components.policyengine.heimdall.shared.SharedMemory;

public class Tester {

	public static final String POLICY_TAG = "SYG";
	
	
	/* ===========================================================
	==============================================================
	==============================================================
	testPolicyExportLocal
	==============================================================
	==============================================================
	=========================================================== */
	public static void testPolicyExportLocal(User currentUser) {
		// parameters to send:
		// - user id
		// - policy home hostname/port
		// - policy text
		// - cesName
		// - cesId
		Vector allParameters = new Vector();
		allParameters.add(new Long(currentUser.getId()));
		allParameters.add(POLICY_TAG);
		allParameters.add(currentUser.getModelByTag(POLICY_TAG).
				getModelAsText());
		allParameters.add("7d");
		
		RemoteCommand command = new RemoteCommand(
				RemoteCommand.LOAD_CES_COMMAND,
				allParameters);
		
		SharedMemory.addToQueue(command, SharedMemory.QUEUE_INFORM_REQ);
//		InboundThread.exportCes(command);
	} // testPolicyExportLocal

	
	/* ===========================================================
	==============================================================
	==============================================================
	testQueryLocal
	==============================================================
	==============================================================
	=========================================================== */
	public static void testQueryLocal(User currentUser, EventObj ce) {
		// parameters to send:
		// - user id
		// - ces tag
		// - cesParametes (Vector)
		Vector allParameters = new Vector();
		allParameters.add(new Long(currentUser.getId()));
		allParameters.add(POLICY_TAG + "_7d");
		
		ModelDefinition currentModelDef = currentUser.getModelByTag(
				POLICY_TAG);
		CesDefinition currentCes = currentModelDef.getCes("7d");
		RuleDefinition cesGlobalRestriction = currentCes.getGlobalRestriction();
		String cesSqlRestriction = 
			cesGlobalRestriction.getSqlRestriction(currentUser, ce);

		allParameters.add(cesSqlRestriction);
		
		RemoteCommand command = new RemoteCommand(
				RemoteCommand.QUERY_CES_COMMAND,
				allParameters);
		
//		Outbound.getAllRecordsAfter(
//				new NodeInfo("argentina.gsd.inesc-id.pt", 8002),
//				currentUser,
//				POLICY_TAG,
//				ce,
//				"7d",
//				"allNodes",
//				Utilities.UNDEFINED_INT_VALUE);
	} // testQueryLocal
	
	
	/* ===========================================================
	==============================================================
	==============================================================
	testQueryRemote
	==============================================================
	==============================================================
	=========================================================== */
	public static void testQueryRemote(User currentUser, EventObj ce) {
		
//		Outbound.getAllRecordsAfter(
//				new NodeInfo("argentina.gsd.inesc-id.pt", 8002),
//				currentUser,
//				POLICY_TAG,
//				ce,
//				"7d",
//				"allNodes",
//				Utilities.UNDEFINED_LONG_VALUE);

	
	
	} // testPolicyExportLocal

	
	/* ===========================================================
	==============================================================
	==============================================================
	testPolicyStore
	==============================================================
	==============================================================
	=========================================================== */
	public static void testPolicyStore(User currentUser) {

		try {
			PolicyManager.storePolicy(currentUser, 
					POLICY_TAG,
					PolicyDAO.getRecord(
							currentUser.getId(), POLICY_TAG).getText());;
		} catch (IOException e) {
			e.printStackTrace();
		}
	} // testPolicyExportRemote

	
	/* ===========================================================
	==============================================================
	==============================================================
	testPolicyExportRemote
	==============================================================
	==============================================================
	=========================================================== */
	public static void testPolicyExportRemote(User currentUser) {

		Outbound.exportCes("argentina", 8001, 
				currentUser, 
				"", 
				"myCompanyStorageActions");
	} // testPolicyExportRemote

	


	
	/* ===========================================================
	==============================================================
	==============================================================
	main
	==============================================================
	==============================================================
	=========================================================== */
	public static void main(String[] args) {

		Runtime rt = Runtime.getRuntime();
		
		System.out.println("-->0: " + (rt.totalMemory() - rt.freeMemory()));
		System.gc();
		System.out.println("-->1: " + (rt.totalMemory() - rt.freeMemory()));

		Vector a = new Vector(1,1);
		for (int i = 0; i < 10000; i++) {
			a.add(new Integer(i));
		}
		
		System.out.println("-->2: " + (rt.totalMemory() - rt.freeMemory()));

		a = null;
		
		System.out.println("-->3: " + (rt.totalMemory() - rt.freeMemory()));
		System.gc();
		System.out.println("-->4: " + (rt.totalMemory() - rt.freeMemory()));

		a = new Vector(1,1);
		for (int i = 0; i < 10000; i++) {
			a.add(new Integer(i));
		}
		
		System.out.println("-->2: " + (rt.totalMemory() - rt.freeMemory()));

		a = null;
		
		System.out.println("-->3: " + (rt.totalMemory() - rt.freeMemory()));
		System.gc();
		System.out.println("-->4: " + (rt.totalMemory() - rt.freeMemory()));

	} // main
}; // MainConsole
